﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using MongoDB.Bson;

namespace MongoExplorerTool.Model
{
    public class Collection : IRefreshable
    {
        #region Properties
        public string Name { get; set; }
        public MongoCollection<BsonDocument> MongoCollection { get; private set; }
        public CollectionStats CollectionStats { get; set; }
        public long TotalDataSize { get; set; }
        public long TotalStorageSize { get; set; }
        #endregion Properties

        #region Instance Methods
        public Collection(MongoCollection<BsonDocument> mongoCollection)
        {
            this.MongoCollection = mongoCollection;
            this.Refresh();
        }

        private CollectionStats GetCollectionStats()
        {
            var mongoStats = this.MongoCollection.GetStats();

            var collectionStats = new CollectionStats();
            collectionStats.DataSize = mongoStats.DataSize;
            collectionStats.MaxDocuments = mongoStats.MaxDocuments;
            collectionStats.ObjectCount = mongoStats.ObjectCount;
            collectionStats.StorageSize = mongoStats.StorageSize;

            return collectionStats;
        }

        public void Refresh()
        {
            this.Name = this.MongoCollection.Name;
            this.TotalDataSize = this.MongoCollection.GetTotalDataSize();
            this.TotalStorageSize = this.MongoCollection.GetTotalStorageSize();
            this.CollectionStats = this.GetCollectionStats();
        }
        #endregion Instance Methods
    }
}
